package com.oazon.moco.lan;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.oazon.common.Logger;
import com.oazon.moco.bean.HttpMsgBodyBean;
import com.oazon.moco.lan.core.AbsHttpMsgHandler;
import com.oazon.moco.lan.core.http.bean.JsonRequest;
import com.oazon.moco.lan.handler.ControlHandler;
import com.oazon.moco.lan.handler.QueryHandler;
import com.oazon.moco.lan.handler.SubscribeHandler;

import io.netty.channel.ChannelHandlerContext;

public class HttpMsgManager {

	private static final String TAG = "HttpMsgManager";

	private static HttpMsgManager object = null;
	
	private List<AbsHttpMsgHandler> handlers = new ArrayList<AbsHttpMsgHandler>();

	private HttpMsgManager() { 
		handlers.add(new SubscribeHandler());
		handlers.add(new QueryHandler());
		handlers.add(new ControlHandler());
	};

	public static HttpMsgManager getInstance() {
		if (object == null) {
			object = new HttpMsgManager();
		}
		return object;
	}

	public void doBusiness(Object body, ChannelHandlerContext ctx) {
		
		HttpMsgBodyBean bean = new HttpMsgBodyBean();

		JsonRequest jsonRequest = (JsonRequest) body;

		String uuid = ((JsonRequest) body).getUuid();
		String operation = jsonRequest.getOperation();
		String classification = jsonRequest.getClassification();
		String type = jsonRequest.getType();
		String data = (String) jsonRequest.getData();

		bean.setClassification(classification);
		bean.setCtx(ctx);
		bean.setData(data);
		bean.setOperation(operation);
		bean.setType(type);
		bean.setUuid(uuid);

		Logger.i(TAG, "uuid==" + uuid //
				+ "\r\noperation==" + operation//
				+ "\r\nclassification==" + classification//
				+ "\r\ntype==" + type//
				+ "\r\ndata==" + data);
		
		for (AbsHttpMsgHandler absHttpMsgHandler : handlers) {
			absHttpMsgHandler.doBusiness(bean);
		}
	}
}
